package com.ganbing518.commons.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.MessageDigest;
import java.security.spec.KeySpec;
import java.util.Calendar;
import java.util.Date;

public class DESUtils {

    public static int encryptDESede_CBC_NoPadding(byte[] keyBytes,
                                                  byte[] icv, byte[] input, int intputOffset,
                                                  int inputLen, byte[] output, int outputOffset)
            throws Exception {
        SecretKey secretKey = createSecretKey(keyBytes);
        Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(
                icv);
        cipher.init(1, secretKey, ivParameterSpec);
        return cipher.doFinal(input, intputOffset, inputLen,
                output, outputOffset);
    }

    public static SecretKey createSecretKey(byte[] paramArrayOfByte)
            throws Exception {
        int i = (short) paramArrayOfByte.length;
        return createSecretKey(paramArrayOfByte, i);
    }

    public static SecretKey createSecretKey(byte[] paramArrayOfByte, int paramInt2) throws Exception {
        String str;
        Object localObject1;
        Object localObject2;
        switch (paramInt2) {
            case 8:
                str = "DES";
                localObject2 = paramArrayOfByte;
                if (paramArrayOfByte.length > 8) {
                    localObject2 = new byte[8];
                    System.arraycopy(paramArrayOfByte, 0, localObject2, 0, 8);
                }
                localObject1 = new DESKeySpec((byte[]) localObject2, 0);
                break;
            case 16:
                str = "DESede";
                byte[] arrayOfByte = new byte[24];
                System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, 16);
                System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 16, 8);
                localObject1 = new DESedeKeySpec(arrayOfByte, 0);
                break;
            case 24:
                str = "DESede";
                localObject2 = paramArrayOfByte;
                if (paramArrayOfByte.length > 24) {
                    localObject2 = new byte[24];
                    System.arraycopy(paramArrayOfByte, 0, localObject2, 0, 24);
                }
                localObject1 = new DESedeKeySpec((byte[]) localObject2, 0);
                break;
            default:
                throw new RuntimeException("invalid length of rawKey");
        }
        localObject2 = SecretKeyFactory.getInstance(str);
        return ((SecretKeyFactory) localObject2)
                .generateSecret((KeySpec) localObject1);
    }

    public static void main(String[] args) throws Exception {

        String iv = "0000000000000000";
        String text = "5155C37CAAB87887AB551E593670225C";
        byte[] result = new byte[24];


        encryptDESede_CBC_NoPadding(ByteUtil.hexToByteArray(text), ByteUtil.hexToByteArray(iv), ByteUtil.hexToByteArray("0319F82457A3EA9E"), (short) 0, "0319F82457A3EA9E".length()
                        / 2, result,
                (short) 0);
        String key1 = ByteUtil.byteArrayToHex(result);

        System.out.println(key1);
        byte[] mac = MacUtils.calculatePboc3desMAC(ByteUtil.hexToByteArray("84820300100319F82457A3EA9E"), ByteUtil.hexToByteArray("5155C37CAAB87887AB551E593670225C"), ByteUtil.hexToByteArray("0000000000000000"));

        System.out.println(ByteUtil.byteArrayToHex(mac));

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.DATE, 12);

        Date start = calendar.getTime();
        calendar.set(Calendar.DATE, 14);
        Date end = calendar.getTime();

        Date now = new Date();

        System.out.println();

        if (!end.before(now) && !start.after(now)) {
            System.out.println(1);
        }

        calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 30);
        Date open = calendar.getTime();

        calendar.set(Calendar.HOUR_OF_DAY, 1);
        calendar.set(Calendar.MINUTE, 30);
        calendar.add(Calendar.DATE, 1);

        Date close = calendar.getTime();
        if (!close.before(now) && !open.after(now)) {
            System.out.println(2);
        }
    }

}
